package com.lx.web;

import com.lx.entity.Book;
import com.lx.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.naming.Name;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class BookServlet {
    @Autowired
    private BookService bookService;

    //查询所有图书
    @RequestMapping("/bookList")
    public String bookList(String page, HttpServletRequest request){
        int p = 1; //默认显示第一页
        if (page != null && !"".equals(page)) {
            //特殊字符无法转换，捕获
            try {
                p = Integer.parseInt(page);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
        List<Book> books = bookService.getBook(p, 4);
        int count = bookService.countBook();
        int n = count % 4 ==0 ? count / 4 :count / 4 +1;
        request.setAttribute("pageNumber",n);
        request.setAttribute("page",p);
        request.setAttribute("books",books);
        return "/WEB-INF/jsp/bookList.jsp";
    }

    @RequestMapping("/bookListLike")
    public String getBookByNameLike(String book_name,HttpServletRequest request){
        List<Book> bookByNameLike = bookService.getBookByNameLike(book_name);
        request.setAttribute("books",bookByNameLike);
        return "/WEB-INF/jsp/bookList.jsp";

    }

    //跳转到添加图书页面
    @RequestMapping("/toAddBook")
    public String toAddBook() {
        return "/WEB-INF/jsp/addBook.jsp";
    }

    //添加图书
    @RequestMapping("/addBook")
    public String addBook(String book_name,String book_author,String book_time,
                          String book_content,String book_type){
        Book book = bookService.addBook(book_name, book_author, book_time, book_content, book_type);
        if (book != null){
            return "redirect:/bookList";
        }else {
            return "redirect:/toAddBook";
        }
    }

    //删除
    @RequestMapping("/deleteBook")
    public String deleteBook(int id){
        bookService.deleteBook(id);
        return "redirect:/bookList";
    }

    //修改页
    @RequestMapping("/toUpdateBook")
    public String toUpdateBook(int id,HttpServletRequest request){
        Book bookById = bookService.getBookById(id);
        System.out.println(bookById);
        request.setAttribute("bookById",bookById);
        return "/WEB-INF/jsp/updateBook.jsp";
    }

    //修改
    @RequestMapping("/updateBook")
    public String updateBook(int id,String book_name,String book_author,String book_time,
                             String book_content,String book_type){
        //将String的日期转换为Data类型
        Date parse = null;
        try {
            parse = new SimpleDateFormat("yyyy-MM-dd").parse(book_time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Book book = new Book();
        book.setBook_name(book_name);
        book.setBook_author(book_author);
        book.setBook_time(parse);
        book.setBook_content(book_content);
        book.setBook_type(book_type);
        book.setId(id);
        boolean b = bookService.updateBook(book);
        if (b){
            return "redirect:/bookList";
        }else {
            return "redirect:/updateBook";
        }

    }
}
